package com.starlight.ts_clouddev.controller;

import com.starlight.ts_clouddev.annotition.AuthenticationCheck;
import com.starlight.ts_clouddev.annotition.RoleType;
import com.starlight.ts_clouddev.command.CalculateRenewCommand;
import com.starlight.ts_clouddev.infrastructure.entity.CloudInstance;
import com.starlight.ts_clouddev.infrastructure.entity.CloudUser;
import com.starlight.ts_clouddev.service.CloudInstanceService;
import com.starlight.ts_clouddev.service.CloudUserService;
import com.starlight.ts_clouddev.util.CloudResponse;
import com.starlight.ts_clouddev.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/5/18 15:11
 */
@RestController
@RequestMapping("/instance")
public class CloudInstanceController {

    @Resource
    private CloudInstanceService cloudInstanceService;

    @Resource
    private CloudUserService cloudUserService;


    @PostMapping ("/create")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> create(CloudInstance cloudInstance){
        Long userId= Long.valueOf(CurrentUserInfoUtils.getUserId());
        cloudInstance.setUserId(userId);
        Long cost=cloudInstanceService.calculateUpgradeTotalCharge(cloudInstance);
        CloudUser cloudUser=cloudUserService.getById(userId);
        Long account=cloudUser.getAccount();
        if (account<cost){
            throw new RuntimeException("账户余额不足！");
        }
        account-=cost;
        cloudUser.setAccount(account);
        cloudUserService.updateById(cloudUser);
        return CloudResponse.success(cloudInstanceService.save(cloudInstance));
    }

    @PostMapping("/update")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> update(CloudInstance cloudInstance){
        System.out.println(cloudInstance.toString());
        return CloudResponse.success(cloudInstanceService.updateById(cloudInstance));
    }

    @GetMapping("/read")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<List<CloudInstance>> read(){
        Long userId= Long.valueOf(CurrentUserInfoUtils.getUserId());
        return CloudResponse.success(cloudInstanceService.listByUserId(userId));
    }

    @PostMapping("/delete")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> delete(CloudInstance cloudInstance){
        System.out.println(cloudInstance.toString());
        return CloudResponse.success(cloudInstanceService.removeById(cloudInstance));
    }

    @PostMapping("/calculateUpgrade")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Long> calculateUpgrade(CloudInstance cloudInstance){
        return CloudResponse.success(cloudInstanceService.calculateUpgradeTotalCharge(cloudInstance));
    }

    @PostMapping("/calculateRenew")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Long> calculateRenew(CalculateRenewCommand command){
        Long cloudInstanceId=command.getCloudInstanceId();
        Long deltaTime=command.getDeltaTime();
        System.out.println("续期"+command);
        return CloudResponse.success(cloudInstanceService.calculateRenewTotalCharge(cloudInstanceId,deltaTime));
    }

    @PostMapping("/upgrade")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> upgrade(CloudInstance cloudInstance){
        Long userId= Long.valueOf(CurrentUserInfoUtils.getUserId());
        Long cost=cloudInstanceService.calculateUpgradeTotalCharge(cloudInstance);
        CloudUser cloudUser=cloudUserService.getById(userId);
        Long account=cloudUser.getAccount();
        if (account<cost){
            throw new RuntimeException("账户余额不足！");
        }
        account-=cost;
        cloudUser.setAccount(account);
        cloudUserService.updateById(cloudUser);
        return CloudResponse.success(cloudInstanceService.updateById(cloudInstance));
    }

    @PostMapping("/renew")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> renew(CalculateRenewCommand command){
        Long cloudInstanceId=command.getCloudInstanceId();
        Long deltaTime=command.getDeltaTime();
        long  timeMillis=deltaTime*30*24*60*60*1000;
        Long userId= Long.valueOf(CurrentUserInfoUtils.getUserId());
        Long cost=cloudInstanceService.calculateRenewTotalCharge(cloudInstanceId,deltaTime);
        CloudUser cloudUser=cloudUserService.getById(userId);
        Long account=cloudUser.getAccount();
        if (account<cost){
            throw new RuntimeException("账户余额不足！");
        }
        account-=cost;
        cloudUser.setAccount(account);
        cloudUserService.updateById(cloudUser);
        CloudInstance cloudInstance=cloudInstanceService.getById(cloudInstanceId);
        long oldDeadLine=cloudInstance.getDeadline().getTime();
        Date newDeadLine=new Date(oldDeadLine+timeMillis);
        cloudInstance.setDeadline(newDeadLine);
        return CloudResponse.success(cloudInstanceService.updateById(cloudInstance));
    }
}
